var isfulladress, isAccount, isPwd, isEmail, isCorpname, isName, isCorpidcard = true, isTelphone, isCity = false, isSms = false, isProtocol;
var send = 0;
var num = "";
var pwd;
$('input[name=account]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("账号为6-20位任意非空格字符！").css("color", "#ff5722");
        isAccount = false;
    } else {
        new Ajax("check_account.jsp").keyvalue("account", text).get(function (data) {
            var exist = eval(data);
            if (!exist) {
                $('input[name=account]').next().text("√ 输入账号有效").css("color", "#5ce078");
                isAccount = true;
            } else {
                $('input[name=account]').next().text("账号已存在！").css("color", "#fe7451");
            }
        });
    }
    jiance();
});
$('input[name=pwd]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{6,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("密码为6-20位任意非空格字符！").css("color", "#ff5722");
        isPwd = false;
    } else {
        $(this).next().text("√ 输入密码有效").css("color", "#5ce078");
        isPwd = true;
    }
    jiance();
});
$('input[name=email]').blur(function () {
    var text = $(this).val();
    var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (!pattern.test(text)) {
        $(this).next().text("请输入正确的邮箱账号！").css("color", "#ff5722");
        isEmail = false;
    } else {
        $('input[name=email]').next().text("√ 输入邮箱有效").css("color", "#5ce078");
        isEmail = true;
    }
    jiance();
});
$('input[name=corpname]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{1,20}$/;
    if (!pattern.test(text)) {
        $(this).next().text("不可以包含空字符！").css("color", "#ff5722");
        isCorpname = false;
    } else {
         $(this).next().text("√ 企业名称有效").css("color", "#5ce078");
        isCorpname = true;
    }
    jiance();
});
$('input[name=linkname]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{1,10}$/;
    if (!pattern.test(text)) {
        $(this).next().text("请输入联系人姓名！").css("color", "#ff5722");
        isName = false;
    } else {
        $(this).next().text("√ 输入姓名有效").css("color", "#5ce078");
        isName = true;
    }
    jiance();
});
$('input[name=corpidcard]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{13,18}$/;
    if (pattern.test(text)) {
        if (text) {
            new Ajax("check_account.jsp").keyvalue("corpidcard", text).get(function (data) {
                var eva = eval(data);
                if (eva) {
                    $('input[name=corpidcard]').next().text("社会信用代码重复！").css("color", "#fe7451");
                    isCorpidcard = false;
                } else {
                    $('input[name=corpidcard]').next().text("社会信用代码有效！").css("color", "#5ce078");
                    isCorpidcard = true;
                }
            })
        }
    } else {
        $('input[name=corpidcard]').next().text("社会信用代码为13-18位！").css("color", "#fe7451");
        isCorpidcard = false;
    }
    jiance();
});
$('input[name=telphone]').blur(function () {
    var text = $(this).val();
    var pattern = /^1[34578]\d{9}$/;
    if (!pattern.test(text)) {
        $(this).next().text("请输入正确的手机号码！").css("color", "#ff5722");
        isTelphone = false;
        send = 0;
        $('#seccode').css("background", "#cbcbcb");
    } else {
        $(this).next().text("√ 输入手机号有效").css("color", "#5ce078");
        $('#seccode').css("background", "#1e9fff");
        send = 1;
        isTelphone = true;
    }
    jiance();
});
//点击发送短信
$('#seccode').click(function () {
    $(this).css("background", "#cbcbcb");
    var phone = $('input[name=telphone]').val();
    var i = 60;
    if (send == 1) {
        send = 0;
        new Ajax("seccode.jsp").keyvalue("phone", phone).post(function (data) {
            var iCount = setInterval(function () {
                i--;
                $('#seccode').text("(" + i + ")重新发送");
                if (i == 0) {
                    send = 1;
                    $('#seccode').text("获取短信验证码");
                    $('#seccode').css("background", "#1e9fff");
                    clearInterval(iCount);
                }
            }, 1000);
        });
    }
});
$('input[name=fulladress]').blur(function () {
    var text = $(this).val();
    var pattern = /^\S{1,50}$/;
    if (!pattern.test(text)) {
        $(this).next().text("不可以包含空格！").css("color", "#ff5722");
        isfulladress = false;
    } else {
        $(this).next().text("√ 企业详细地址有效").css("color", "#5ce078");
        isfulladress = true;
    }
    jiance();
});


$('input[name=note]').blur(function () {
    var note = MD5($(this).val());
    var cook = Cookie.Get("key");
    if (note == cook) {
        isSms = true;
        $(this).next().next().text("√ 验证成功").css("color", "#5ce078");
    } else {
        isSms = false;
        $(this).next().next().text("输入的验证码有误！").css("color", "#ff5722");
    }
    jiance();
});
$('input[name=agreement]').click(function () {
    if ($(this).is(":checked")) {
        isProtocol = true;
    } else {
        isProtocol = false;
    }
    jiance();
    
});
function jiance() {
    var len = $('input[name=corpidcard]').val();
    if (len.length == 0) {
        isCorpidcard = true;
    }
//    if (isAccount && isPwd && isEmail && isCorpname && isName && isTelphone && isCorpidcard && isfulladress  && isSms && isProtocol) {
if (isAccount && isPwd && isEmail && isCorpname && isName  && isCorpidcard && isfulladress  && isProtocol) {
        $('button').css("background", "#699ae4");
    } else {
        $('button').css({"background": "#f0f0f0", "color": "#fff"});
    }
}
//短信验证码
var validate = new Validated();
$('button').click(function () {
    var len = $('input[name=corpidcard]').val();
    if (len.length == 0) {
        isCorpidcard = true;
    }
//    if (isAccount && isPwd && isEmail && isCorpname && isName && isTelphone && isCorpidcard && isfulladress && isSms && isProtocol) {
 if (isAccount && isPwd && isEmail && isCorpname && isName && isCorpidcard && isfulladress && isProtocol) {
       var dat =  $("#form").serializeArray();
       dat.push({'name':'enterprise','value':$('#enterprise').text()});
        new Ajax("/login/registers").keyvalue(dat).getJson(function (json) {
            var js = json.data
            switch(js){
                case 100:
                    $('#sele').show();
                    $('#sele').find("p").text("输入的账号有误");
                    break;
                    case 101:
                    $('#sele').show();
                    $('#sele').find("p").text("密码格式不正确");
                    break;
                    case 102:
                    $('#sele').show();
                    $('#sele').find("p").text("公司名称格式不正确");
                    break;
                    case 103:
                    $('#sele').show();
                    $('#sele').find("p").text("联系人姓名格式不正确");
                    break;
                    case 104:
                    $('#sele').show();
                    $('#sele').find("p").text("主营类别格式不正确");
                    break;
                    case 105:
                    $('#sele').show();
                    $('#sele').find("p").text("联系方式格式不正确");
                    break;
                    case 107:
                    $('#sele').show();
                    $('#sele').find("p").text("详细地址格式不正确");
                    break;
                    case 109:
                    $('#sele').show();
                    $('#sele').find("p").text("个人账号已存在");
                    break;
                    case 110:
                    $('#sele').show();
                    $('#sele').find("p").text("手机号已被注册");
                    break;
                    default:
                    $('#sele').show().attr("str","1");
                    $('#sele').find("p").text("您的企业账号为:"+json.data); 
            }
        });
    }
});
//点击显示协议
$('#protocol').next().click(function () {
    $('#vag').show();
});
$('#vag').click(function (e) {
    $(this).hide();
})
$('#aler').click(function (e) {
    e.stopPropagation();
});
var enterprise;
new Ajax("/api/user/enterprise").getJson(function (json) {
    $.each(json, function (k, v) {
      enterprise = v["myid"];
      enterprise = parseInt(enterprise)+1;  
      $('#enterprise').text(enterprise);
    })
})
$('#confirm').click(function(){
   var s = $('#sele').attr("str");
   if(s=="1"){
       document.location = "login.jsp";
       $('#sele').attr("str","0");
   }else{
       $('#sele').hide();
   }
});







